package com.rico.deploy.controller;

import com.github.pagehelper.PageInfo;
import com.rico.common.response.MessageUtils;
import com.rico.common.response.RestResponse;
import com.rico.deploy.entity.College;
import com.rico.deploy.service.CampusService;
import com.rico.deploy.service.CollegeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 吴泽胜
 * @FileName CollegeController
 * @date 2021-05-03 7:49 下午
 * @Software: IntelliJ IDEA
 */

@Controller
@Slf4j
public class CollegeController {

    @Autowired
    private CollegeService collegeService;

    @Autowired
    private CampusService campusService;

    /**
     * 默认获取第一页学院列表
     * @return
     */
    @GetMapping("college/list")
    @ResponseBody
    public RestResponse getlist(Integer page, Integer limit) {
        log.info("{} {}", page, limit );
        List<College> colleges = collegeService.selectAll(page, limit);
        PageInfo<College> collegesPageInfo = new PageInfo(colleges);
        return MessageUtils.success(collegesPageInfo.getList(), (int) collegesPageInfo.getTotal());
    }

    @GetMapping("college/lists")
    @ResponseBody
    public RestResponse getlists() {
        return MessageUtils.success(collegeService.selectAll());
    }


    @GetMapping("college/delete")
    @ResponseBody
    public RestResponse deleteById(Integer id) {
        log.info("{}", id);
        String msg = null;
        if ( collegeService.deleteCollegeById(id) == 1 )
            msg = "删除成功";
        else
            msg = "删除失败";
        return MessageUtils.success(msg);
    }

    /**
     * 更新数据
     * @param college
     * @return
     */
    @PostMapping("college/put")
    @ResponseBody
    public RestResponse put(College college) {
        college.setCampusCode(campusService.selectByCampusName(college.getCampus_name()));
        int i = collegeService.updateCollege(college);
        if ( i == 0 )
            return MessageUtils.fail("编辑失败");

        else
            return MessageUtils.success("编辑成功");
    }

    /**
     * 添加数据
     * @param college
     * @return
     */
    @PostMapping("college/post")
    @ResponseBody
    public RestResponse post(College college) {
        college.setCampusCode(campusService.selectByCampusName(college.getCampus_name()));
        log.info("{}", college.toString());
        int i = collegeService.insertCollege(college);
        if ( i == 0 )
            return MessageUtils.fail("添加失败");

        else
            return MessageUtils.success("添加成功");
    }

    /**
     * 获取html页面
     * @return
     */
    @GetMapping("collegeManage")
    public String gethtml(){
        return "page/collegeManage";
    }

}
